﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MoneyWentWhere.DataModel;
using BrightIdeasSoftware;

namespace MoneyWentWhere.WinFormsUI.UI
{
    public partial class AnalysisPanel : UserControl
    {
        public AnalysisPanel()
        {
            InitializeComponent();
        }

        public MoneyDataSet DataSet { get; set; }

        public void UpdateTransactions()
        {
            var allTx = DataSet.Accounts.SelectMany(ac => ac.Transactions);
            transactionList.SetObjects(allTx);

        }

        const string AmountFormat = @"{0,10:#,##0;(#,##0);'0'}";

        private void bApplyFilter_Click(object sender, EventArgs e)
        {
            transactionList.ModelFilter = CompositeFilter.Create(tbFilter.Text, transactionList);
            transactionList.UseFiltering = true;

            // stats
            StringBuilder sb = new StringBuilder();

            var filteredTx = transactionList.FilteredObjects.Cast<Transaction>();
            if (filteredTx.Any())
            {
                DateTime start = filteredTx.Min(tx => tx.Date);
                DateTime end = filteredTx.Max(tx => tx.Date);
                double totalDays = (end - start).TotalDays;
                double totalMonths = totalDays / 30.4;
                sb.AppendFormat("From: {0:yyyy-MM-dd} to: {1:yyyy-MM-dd} months: {2:#,0}", start, end, totalMonths);
                sb.AppendLine();

                decimal totalIn = filteredTx.Where(tx => tx.DcAmount > 0).Sum(tx => tx.DcAmount);
                sb.AppendFormat("      Total in: " + AmountFormat, totalIn);
                sb.AppendLine();
                sb.AppendFormat("  Avg in/month: " + AmountFormat, ((double)totalIn / totalMonths));
                sb.AppendLine();

                decimal totalOut = filteredTx.Where(tx => tx.DcAmount < 0).Sum(tx => tx.DcAmount);
                sb.AppendFormat("     Total out: " + AmountFormat, totalOut);
                sb.AppendLine();
                sb.AppendFormat(" Avg out/month: " + AmountFormat, ((double)totalOut / totalMonths));
                sb.AppendLine();
            }

            tbStats.Text = sb.ToString();
        }

        private void transactionList_DoubleClick(object sender, EventArgs e)
        {
            Transaction tx = transactionList.GetSelectedObject() as Transaction;
            if (tx == null) { return; }


            if (NavigateToTransaction != null)
            {
                NavigateToTransaction(this, new TransactionEventArgs(tx));
            }
        }

        public event EventHandler<TransactionEventArgs> NavigateToTransaction;

    }

    public class TransactionEventArgs : EventArgs
    {
        public readonly Transaction Transaction;
        public TransactionEventArgs(Transaction tx)
        {
            Transaction = tx;
        }

    }
}
